package howl.model;

import howl.types.AccessGroup;
import howl.types.HowlUser;
import howl.types.TableDescriptor;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.users.User;

public class FeedAuthentication {

	public static void addAccess(TableDescriptor td, HowlUser u) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery("select from howl.types.AccessGroup "
				+ "where FeedID == Param0 && UserID == Param1 "
				+ "parameters Long Param0, Long Param1 "
				+ "import howl.types.AccessGroup;");

		List<AccessGroup> ag = (List<AccessGroup>) query.execute(td.getId(), u
				.getId());
		if (ag.size() == 0) {
			AccessGroup a = new AccessGroup(u.getId(), td.getId());
			pm.makePersistent(a);
			pm.close();
		}
	}

	public static void removeAccess(TableDescriptor td, HowlUser u) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery("select from howl.types.AccessGroup "
				+ "where FeedID == Param0 && UserID == Param1 "
				+ "parameters Long Param0, Long Param1 "
				+ "import howl.types.AccessGroup;");

		/*
		 * List<AccessGroup> results = (List<AccessGroup>) query.execute(td
		 * .getId(), u.getId()); pm.deletePersistentAll(results);
		 */

		query.deletePersistentAll(td.getId(), u.getId());
	}

	public static void removeAllAccess(TableDescriptor td) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery("select from howl.types.AccessGroup "
				+ "where FeedID == Param0 " + "parameters Long Param0 "
				+ "import howl.types.AccessGroup;");

		query.deletePersistentAll(td.getId());

	}

	public static List<HowlUser> getAccessor(TableDescriptor td) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery("select from howl.types.AccessGroup "
				+ "where FeedID == Param1 " + "parameters Long Param1 "
				+ "import howl.types.AccessGroup;");

		List<AccessGroup> results = (List<AccessGroup>) query.execute(td
				.getId());

		ArrayList<HowlUser> l = new ArrayList<HowlUser>();

		Query Tablequery = pm.newQuery("select from howl.types.HowlUser "
				+ "where  id == Param " + "parameters Long Param "
				+ "import howl.types.HowlUser;");

		for (int i = 0; i < results.size(); i++) {
			List<HowlUser> Tableresults = (List<HowlUser>) query
					.execute(results.get(i).getFeedID());
			l.addAll(Tableresults);
		}

		return l;

	}

	public static List<TableDescriptor> getAvailable(HowlUser u) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery("select from howl.types.AccessGroup "
				+ "where UserID == Param1 " + "parameters Long Param1 "
				+ "import howl.types.AccessGroup;");

		List<AccessGroup> results = (List<AccessGroup>) query
				.execute(u.getId());

		ArrayList<TableDescriptor> l = new ArrayList<TableDescriptor>();

		Query Tablequery = pm
				.newQuery("select from howl.types.TableDescriptor "
						+ "where  id == Param " + "parameters Long Param "
						+ "import howl.types.TableDescriptor;");

		for (int i = 0; i < results.size(); i++) {
			List<TableDescriptor> Tableresults = (List<TableDescriptor>) Tablequery
					.execute(results.get(i).getFeedID());
			for (int j = 0; j < Tableresults.size(); j++) {
				l.add(Tableresults.get(j));
			}
		}

		return l;
	}

	public static boolean hasAccess(TableDescriptor td, HowlUser u) {

		List<TableDescriptor> s = getAvailable(u);

		for (int i = 0; i < s.size(); i++) {
			if (s.get(i).getId().equals(td.getId())) {
				return true;
			}
		}
		return false;
	}

	public static HowlUser getHowlUserfromUser(String u) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery("select from howl.HowlUser "
				+ "where emailAddress == Param " + "parameters String Param "
				+ "import howl.HowlUser;");
		List<HowlUser> results = (List<HowlUser>) query.execute(u);

		return results.get(0);
	}

}
